#include "stdafx.h"
#include "CommonResourceProvider.h"
#include "IPALDebuggingServices.h"
#include "EncodedPtr.h"
#include "IPlatformServices.h"
#include "MsResourceHelpers.h"


extern EncodedPtr<IPlatformServices> gps;


HRESULT CommonResourceProvider::Create( CommonResourceProvider **ppResourceProvider )
{

	int baseUriLength = 2084;
	CommonResourceProvider *pResourceProvider = NULL;
	HRESULT hr = S_OK;
	WCHAR baseUri[2085];
	WCHAR applicationDirectory[MAX_PATH];

	applicationDirectory[0] = NULL;
	memset(&applicationDirectory[1], NULL, (MAX_PATH - 1) * 2);

	baseUri[0] = NULL;
	memset(&baseUri[1], NULL, 4186);

	pResourceProvider = new CommonResourceProvider();
	if ( pResourceProvider )
	{
		if ( GetModuleFileNameW(NULL, (LPWSTR )applicationDirectory, MAX_PATH)
			|| ( hr = HRESULT_FROM_WIN32(GetLastError()), SUCCEEDED(hr)) )
		{
			hr = UrlCreateFromPathW((LPCWSTR )applicationDirectory, (LPWSTR )baseUri, (LPDWORD)&baseUriLength, 0);
			if ( SUCCEEDED(hr) )
			{
				hr = gps->UriCreate(baseUriLength, baseUri, &pResourceProvider->m_pBaseUri);
				if ( SUCCEEDED(hr) )
				{
					if ( pResourceProvider->m_pBaseUri )
					{
						*ppResourceProvider = pResourceProvider;
						pResourceProvider = NULL;
					}
					else
					{
						hr = 0x8007000Eu;
						if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
						{
							GetPALDebuggingServices()->XcpTrace(
								5u,
								L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
								41,
								hr,
								L"pResourceProvider->m_pBaseUri",
								0);
						}
					}
				}
				else
				{
					if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
					{
						GetPALDebuggingServices()->XcpTrace(
							4u,
							L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
							40,
							hr,
							L"gps->UriCreate(baseUriLength, baseUri, &pResourceProvider->m_pBaseUri)",
							0);
					}
				}
			}
			else
			{

				if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
				{
					GetPALDebuggingServices()->XcpTrace(
						4u,
						L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
						38,
						hr,
						L"UrlCreateFromPath(applicationDirectory, baseUri, &baseUriLength, NULL)",
						0);
				}
			}
		}
		else
		{
			if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
			{
				GetPALDebuggingServices()->XcpTrace(
					4u,
					L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
					35,
					hr,
					L"HRESULT_FROM_WIN32(GetLastError())",
					0);
			}
		}
	}
	else
	{
		hr = 0x8007000Eu;
		if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
		{
			GetPALDebuggingServices()->XcpTrace(
				5u,
				L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
				30,
				hr,
				L"pResourceProvider",
				0);
		}
	}
	if ( pResourceProvider )
	{
		pResourceProvider->Release();
		pResourceProvider = NULL;
	}
	return hr;
}

CommonResourceProvider::~CommonResourceProvider()
{
	if(this->m_pBaseUri)
	{
		delete this->m_pBaseUri;
	}
	delete this;
}

XUINT32 CommonResourceProvider::AddRef()
{
	return CReferenceCount::AddRef();
}

XUINT32 CommonResourceProvider::Release()
{
	return CReferenceCount::Release();
}


HRESULT CommonResourceProvider::GetPropertyBag(const struct IPALUri * pUri, xpairlist<xshared_ptr<CXString>,xshared_ptr<CXString> > * * ppPropertyBag)
{

	HRESULT hr = S_OK;


	*ppPropertyBag = new xpairlist<xshared_ptr<CXString>,xshared_ptr<CXString> >();

	if ( !(*ppPropertyBag))
	{
		hr = 0x8007000Eu;

		if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
		{
			GetPALDebuggingServices()->XcpTrace(
				5u,
				L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
				141,
				0x8007000Eu,
				L"*ppPropertyBag",
				0);
		}
	}
	return hr;
}

HRESULT CommonResourceProvider::TryGetFilePath(const struct IPALUri * pUri, CXString * * ppFilePath)
{
	NOTIMPL
	/*
	HRESULT hr = S_OK;
	CXString * pResourceMap = NULL;
	CXString *pFileRelativePath = NULL;
	IPALUri *pResourceUri = NULL;
	XCHAR * pszPath = NULL;
	unsigned int nPath = 0;
	CXString *pCanonicalUri = NULL;
	unsigned int isMsResourceUri = 0;
	*ppFilePath = NULL;
	hr = MsResourceHelpers::IsMsResourceUri(pUri,&isMsResourceUri);
	if ( SUCCEEDED(hr) )
	{
	if ( isMsResourceUri )
	{
	hr = pUri->GetCanonical(&pCanonicalUri);
	if ( SUCCEEDED(hr) )
	{
	hr = MsResourceHelpers::CrackMsResourceUri(pUri, &pResourceMap, 0, &pFileRelativePath);
	if ( SUCCEEDED(hr) )
	{
	if ( pFileRelativePath )
	{

	hr = m_pBaseUri->Combine(pFileRelativePath->GetCount(), pFileRelativePath->GetBuffer(), &pResourceUri);

	if ( SUCCEEDED(hr) )
	{
	hr = pResourceUri->GetFilePath(&nPath, NULL);

	if ( SUCCEEDED(hr) )
	{
	++nPath;
	pszPath = (XCHAR * )new char[(2 * nPath | -((unsigned __int64)nPath >> 31 != 0))];
	if ( pszPath )
	{
	hr = pResourceUri->GetFilePath(&nPath, pszPath);
	if ( SUCCEEDED(hr) )
	{
	if ( GetFileAttributesW((LPCWSTR )pszPath) != -1 )
	{
	*ppFilePath = CXString::CloneBuffer((const XCHAR * )pszPath, nPath, nPath, TRUE);
	if ( *ppFilePath )
	{
	if ( GetPALDebuggingServices()->GetTraceFlags() & 1 )
	{
	GetPALDebuggingServices()->XcpTrace(
	3,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	107,
	0,
	0,
	L"\"L\\\"CommonResourceProvider: %s -> %s\\\", pCanonicalUri->GetBuffer(), pszPath\"",
	L"CommonResourceProvider: %s -> %s",
	pCanonicalUri->GetBuffer(),
	pszPath);
	}
	}
	else
	{
	hr = 0x8007000Eu;
	if (GetPALDebuggingServices()->GetTraceFlags() & 2 )
	{
	GetPALDebuggingServices()->XcpTrace(
	5u,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	106,
	hr,
	L"*ppFilePath",
	0);
	}
	}
	}
	}
	else
	{
	if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
	{
	GetPALDebuggingServices()->XcpTrace(
	4u,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	97,
	hr,
	L"pResourceUri->GetFilePath(&nPath, pszPath)",
	0);
	}
	}
	}
	else
	{
	hr = 0x8007000Eu;

	if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
	{
	GetPALDebuggingServices()->XcpTrace(
	5u,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	96,
	hr,
	L"pszPath",
	0);
	}
	}
	}
	else
	{
	if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
	{
	GetPALDebuggingServices()->XcpTrace(
	4u,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	93,
	hr,
	L"pResourceUri->GetFilePath(&nPath, NULL)",
	0);
	}
	}
	}
	else
	{
	if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
	{
	GetPALDebuggingServices()->XcpTrace(
	4u,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	91,
	hr,
	L"m_pBaseUri->Combine(pFileRelativePath->GetCount(), pFileRelativePath->GetBuffer(), &pResourceUri)",
	0);
	}
	}
	}
	else
	{
	hr = E_INVALIDARG;
	if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
	{
	GetPALDebuggingServices()->XcpTrace(
	4u,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	89,
	hr,
	L"E_INVALIDARG",
	0);
	}
	}
	}
	else
	{

	if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
	{
	GetPALDebuggingServices()->XcpTrace(
	4u,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	86,
	hr,
	L"MsResourceHelpers::CrackMsResourceUri(pUri, &pResourceMap, NULL, &pFileRelativePath)",
	0);
	}
	}
	}
	else
	{
	if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
	{
	GetPALDebuggingServices()->XcpTrace(
	4u,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	84,
	hr,
	L"pUri->GetCanonical(&pCanonicalUri)",
	0);
	}
	}
	}
	else
	{
	hr = E_INVALIDARG;

	if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
	{
	GetPALDebuggingServices()->XcpTrace(
	4u,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	81,
	hr,
	L"E_INVALIDARG",
	0);
	}
	}
	}
	else
	{
	if ( GetPALDebuggingServices()->GetTraceFlags() & 2 )
	{
	GetPALDebuggingServices()->XcpTrace(
	4u,
	L"windows\\dxaml\\xcp\\plat\\win\\desktop\\commonresourcemanager.cpp",
	78,
	hr,
	L"MsResourceHelpers::IsMsResourceUri(pUri, &isMsResourceUri)",
	0);
	}
	}


	if ( pResourceMap )
	delete pResourceMap;

	if ( pFileRelativePath )
	delete pFileRelativePath;

	delete[] pszPath;

	if ( pCanonicalUri )
	delete pCanonicalUri;
	if ( pResourceUri )
	{
	pResourceUri->Release();
	pResourceUri = NULL;
	}
	return hr;*/
}

CommonResourceProvider::CommonResourceProvider()
{
	m_pBaseUri = NULL;
}

HRESULT CommonResourceProvider::GetString(const CXString & key, CXString * * ppString)
{

	NOTIMPL

}


